<?php
namespace app\controller;


use app\service\MemberService;
use app\service\MemberWalletService;
use app\service\OrderDistributionService;
use app\service\PromotionPosterService;
use const support\CORRECT;
use const support\DENY;
use const support\ERROR;
use const support\LIMIT;
use const support\PAGE;

class DistributionController
{
    //我的邀请
    public function myInvitation(): \support\Response
    {
        try {
            //接收前端传参
            $params=getRequestData([
                'page'	=>PAGE,
                'limit'	=>LIMIT
            ]);
            $lists=MemberService::memberRelations(getMemberId(),$params['page'],$params['limit']);
            return buildSuccess($lists);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //分销中心
    public function userDistributionData(): \support\Response
    {
        try {
            //获取分销相关数据
            $info=OrderDistributionService::getUserDistributionCenterData(getMemberId());
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //分销订单
    public function distributionOrder(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'page'	=>PAGE,
            'limit'	=>LIMIT,
            'type'  =>0
        ]);
        try {
            $info=OrderDistributionService::getOrderDistribution(getMemberId(),$params['type'],$params['page'],$params['limit']);
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //佣金明细
    public function userCommissionLists(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'page'	=>PAGE,
            'limit'	=>LIMIT
        ]);
        try {
            $lists=MemberWalletService::CommissionDetails(getMemberId(),$params['page'],$params['limit']);
            return buildSuccess($lists);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //分销推广信息
    public function myPromotionPoster(): \support\Response
    {
        try {
            $urls=PromotionPosterService::getPromotionPoster(getMemberId());
            return buildSuccess($urls);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //申请成为分销官
    public function toApplyDistribution(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'apply_name'	=>'',
            'apply_mobile'	=>''
        ]);
        try {
            list($retCode,$msg,$data)=MemberService::applyDistributionActivation(getMemberId(),$params['apply_name'],$params['apply_mobile']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //检测用户是否具备分销权限
    public static function checkUserDistributionAuth(): \support\Response
    {
        try {
            $ret=MemberService::checkUserDistributionAuth(getMemberId());
            return buildSuccess([
                'is_auth'=>$ret?CORRECT:DENY
            ]);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

}